package com.sunyard.layoutit.resource;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.restlet.data.LocalReference;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.freemarker.TemplateRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunyard.layoutit.application.SimpleDefaultApplication;
import com.sunyard.layoutit.common.ApplicationConfig;
import com.sunyard.layoutit.dao.DaoFactory;
import com.sunyard.layoutit.pojo.ComponentBoxPojo;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;

/**
 * 页面设计
 * @author gdl
 */
public class LayoutitResource extends BasicResource {
	
	private static final Logger logger = LoggerFactory.getLogger(LayoutitResource.class);

	
	@Get
    public Representation  goTemplate() throws SQLException  {
		//获取freemarker模版
    	String uri = ApplicationConfig.URI_LAYOUTIT ;
    	Configuration config = ApplicationConfig.getFreemarkerConfig() ;
    	Map<String,List<ComponentBoxPojo>> groupboxes = DaoFactory.getRepositoryBoxDao().queryAllGroupPojo() ;
    	logger.info("Box组件分组个数 {}",groupboxes.size());
    	Map<String, Object> dataModel = new HashMap<String, Object>();
    	dataModel.put("basePath", getBasePath());
    	dataModel.put("groupboxes", groupboxes);
    	
    	TemplateRepresentation result = new TemplateRepresentation(uri,config,MediaType.TEXT_HTML) ;
    	result.setDataModel(dataModel);
    	result.setCharacterSet(ApplicationConfig.REST_CHARACTER_SET);
    	getResponse().setStatus(Status.SUCCESS_OK);
    	getBasePath();
		return result ;
	}
}
